<?php
include 'includes/application_top.inc';
 

//$select =  "SELECT shorturl_code,  COUNT(*) AS `user_count` FROM track_url WHERE ";
//$select =  "SELECT t_id,msisdn,useragent,os,shorturl_code,long_url, count(long_url) as urlcount,date(datetime) as datetimeprocess, HOUR(datetime) AS Hour, COUNT(*) AS `user_count` FROM track_url WHERE ";
					$datepickerstart = date("Y-m-d 00:00:00",strtotime(@$_REQUEST['datepickerstart']));
                                        $datepickerend = date("Y-m-d 24:59:59",strtotime(@$_REQUEST['datepickerend']));	

//						    $select = "SELECT * FROM track_url  WHERE ";
                                           $select =  "SELECT shorturl_code,  COUNT(*) AS `user_count` FROM track_url WHERE 1 ";
                                                if(@trim($_REQUEST['datepickerstart'])!="" || @trim($_REQUEST['datepickerstart'])!=""){
                                                          $select .=" AND datetime >= '$datepickerstart' AND datetime <= '$datepickerend' ";	
                                                  }


                                        if(@trim($_REQUEST['os']) != "")
                                        {
                                            $os = trim($_REQUEST['os']);
                                            $select .=" AND os = '$os'";
                                        } 

                                        if(trim(@$_REQUEST['handset'])!="")
                                        {
                                            $handset = trim($_REQUEST['handset']);
//                                                    $select .=" AND os = '$handset'";
                                        }

                                        if(@trim($_REQUEST['shorturl_code']) != "")
                                        {
                                            $shortCode = trim($_REQUEST['shorturl_code']);
                                            $shortCode = explode('/', $shortCode);
                                            $cnt = count($shortCode);
                                            $code = $shortCode[$cnt - 1];
                                            $select .=" AND shorturl_code = '$code'";
                                        }

                                        if(@trim($_REQUEST['long_url']) != "")
                                        {
                                            $longUrl = trim($_REQUEST['long_url']);
                                            $select .= " AND long_url LIKE '%$longUrl%' ";
                                        }

                                        if(trim(@$_REQUEST['handset'])!="")
                                        {
                                            $search = trim(@$_REQUEST['handset']);
//                                                    $select .= " AND handset LIKE '%$search%'";
                                        }
                                        if(trim(@$_REQUEST['os'])!="")
                                        {
                                            $search = trim(@$_REQUEST['os']);
                                            $select .= " AND os LIKE '%$search%' ";
                                        }
							 // print_r($_REQUEST);
			        $arrselect = array();
					if(isset($_REQUEST['msisdndisplay'])&& $_REQUEST['msisdndisplay']!=""){$arrselect[] = $_REQUEST['msisdndisplay'];}
					if(isset($_REQUEST['useragentdisplay'])&& $_REQUEST['useragentdisplay']!=""){$arrselect[] = $_REQUEST['useragentdisplay'];}
					if(isset($_REQUEST['osdisplay'])&& $_REQUEST['osdisplay']!=""){$arrselect[] = $_REQUEST['osdisplay'];}
					if(isset($_REQUEST['shorturldisplay'])&& $_REQUEST['shorturldisplay']!=""){$arrselect[] = $_REQUEST['shorturldisplay'];}
					if(isset($_REQUEST['longurldisplay'])&& $_REQUEST['longurldisplay']!=""){$arrselect[] = $_REQUEST['longurldisplay'];}
 
 
                               $selCount= count($arrselect);
 
 					
								$s = 0;
								$groupby = "";
								for($w=0;$w<$selCount;$w++)
								{
								     //$groupby .=$arrselect[$w];
								  
								    if($arrselect[$w]=="msisdndisplay")
										{
										$groupby .=' msisdn';	
										}
								   if($arrselect[$w]=="useragentdisplay")
										{
										$groupby .=' useragent';	
										}
 									if($arrselect[$w]=="osdisplay")
										{
										$groupby .=' os';	
										}
									 if($arrselect[$w]=="shorturldisplay")
										{
										$groupby .='shorturl_code';	
										}
 									if($arrselect[$w]=="longurldisplay")
										{
										$groupby .='long_url';	
										}
									if($w==$s&& $w!=$selCount-1)
									 {
									   $groupby .= ", ";	
								      }
								$s++;}
								
                      $select .= "  AND date(NOW()) OR datetime IS NULL  GROUP by ".$groupby."  ORDER BY  t_id DESC";
					
					   //echo $select;
					   //echo "<br>".$groupby;
  
//die();
$result = mysql_query($select);

$rows = array();
while($r = mysql_fetch_array($result)) {
	$row[0] = $r[0];
	$row[1] = $r[1];
	array_push($rows,$row);
}

print json_encode($rows, JSON_NUMERIC_CHECK);

mysql_close($con);
?> 
